package com.sicpa.web.controller.system;

import com.sicpa.common.annotation.Log;
import com.sicpa.common.core.controller.BaseController;
import com.sicpa.common.core.domain.AjaxResult;
import com.sicpa.common.core.page.TableDataInfo;
import com.sicpa.common.enums.BusinessType;
import com.sicpa.common.utils.SecurityUtils;
import com.sicpa.system.domain.SysNotice;
import com.sicpa.system.service.ISysNoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 通知公告接口
 *
 * @author wph
 */
@Api(tags = "通知公告")
@RestController
@RequestMapping("/system/notice")
public class SysNoticeController extends BaseController {
    @Autowired
    private ISysNoticeService noticeService;

    @Autowired
    public SysNoticeController(ISysNoticeService noticeService) {
        this.noticeService = noticeService;
    }

    /**
     * 新增通知公告
     */
    @PreAuthorize("@ss.hasPermi('system:notice:add')")
    @ApiOperation("新增通知公告")
    @ApiImplicitParam(name = "notice", value = "公告", dataType = "SysNotice")
    @Log(title = "新增通知公告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysNotice notice) {
        notice.setCreateBy(SecurityUtils.getUsername());
        return toAjax(noticeService.insertNotice(notice));
    }

    /**
     * 新增个人对个人消息
     */
    @PreAuthorize("@ss.hasPermi('system:message:add')")
    @ApiOperation("新增个人对个人消息")
    @ApiImplicitParam(name = "notice", value = "消息", dataType = "SysNotice")
    @Log(title = "新增个人对个人消息", businessType = BusinessType.INSERT)
    @PostMapping("/addMessage")
    public AjaxResult addMessage(@Validated @RequestBody SysNotice notice) {
        notice.setCreateBy(SecurityUtils.getUsername());
        return toAjax(noticeService.insertNotice(notice));
    }

    /**
     * 通知公告列表
     */
    @ApiOperation("下发通知公告/查询通知公告列表")
    @GetMapping("/list")
    public TableDataInfo list(SysNotice notice) {
        startPage();
        List<SysNotice> list = noticeService.selectNoticeList(notice);
        return getDataTable(list);
    }

    /**
     * 删除通知公告
     */
    @PreAuthorize("@ss.hasPermi('system:notice:remove')")
    @ApiOperation("删除通知公告")
    @ApiImplicitParam(name = "noticeId", value = "通知公告ID", required = true, dataType = "int", paramType = "path")
    @Log(title = "删除通知公告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{noticeId}")
    public AjaxResult remove(@PathVariable int noticeId) {
        return toAjax(noticeService.deleteNoticeById(noticeId));
    }

    /**
     * 根据通知公告编号获取详细信息
     */
    @ApiOperation("根据通知公告编号获取详细信息")
    @ApiImplicitParam(name = "noticeId", value = "通知公告编号", required = true, dataType = "int", paramType = "path")
    @GetMapping(value = "/{noticeId}")
    public AjaxResult getInfo(@PathVariable int noticeId) {
        return AjaxResult.success(noticeService.selectNoticeById(noticeId));
    }

    /**
     * 修改通知公告
     */
    @PreAuthorize("@ss.hasPermi('system:notice:edit')")
    @ApiOperation("修改通知公告")
    @ApiImplicitParam(name = "notice", value = "修改通知公告", dataType = "SysNotice")
    @Log(title = "修改通知公告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysNotice notice) {
        notice.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(noticeService.updateNotice(notice));
    }

    /**
     * 修改是否已读状态为已读
     */
    @ApiOperation("修改是否已读状态为已读")
    @ApiImplicitParam(name = "noticeId", value = "公告id", dataType = "int")
    @Log(title = "修改是否已读状态为已读", businessType = BusinessType.UPDATE)
    @PutMapping("/editStatus")
    public AjaxResult editStatus(@RequestBody int noticeId) {
        return toAjax(noticeService.updateStatus(noticeId));
    }

    /**
     * 根据姓名模糊查询用户列表
     */
    @ApiOperation("根据姓名模糊查询用户列表")
    @ApiImplicitParam(name = "name", value = "用户昵称", required = true, dataType = "String", paramType = "path")
    @GetMapping("/listUser/{name}")
    public TableDataInfo listUser(@PathVariable String name) {
        List<Map<String, Object>> userList = noticeService.selectUserList(name);
        return getDataTable(userList);
    }

    /**
     * 获取我的未阅读消息数量
     * @return
     */
    @GetMapping("/getMyNoRead")
    public AjaxResult getMyNoRead(){
        return AjaxResult.success(noticeService.getMyNoRead());
    }
}
